package com.magic.smartcity.view.more.models

import com.magic.smartcity.beans.BusLineBean
import com.magic.smartcity.beans.BusLineStopBean
import com.magic.smartcity.beans.BusOrderBean
import com.magic.smartcity.utils.HttpClient
import com.magic.smartcity.view.more.models.services.BusService

/**
 * @description:
 * @create: Create by Magic on 2022/2/23.
 */
class BusModel {

    private val TAG: String = "BusModel"

    private val service = HttpClient().retrofit.create(BusService::class.java)

    /**
     * 获取巴士所有线路
     */
    fun getLineList(success: (List<BusLineBean>) -> Unit, error: (String) -> Unit) {
        service.getLineList().doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.rows)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 获取巴士指定线路
     */
    fun getLineById(lineId: Int, success: (BusLineBean) -> Unit, error: (String) -> Unit) {
        service.getLineById(lineId).doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.data)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 获取所有站点
     */
    fun getLineStationList(success: (List<BusLineStopBean>) -> Unit, error: (String) -> Unit) {
        service.getLineStationList().doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.rows)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 获取巴士指定线路的所有站点
     */
    fun getLinesStationById(
        lineId: Int,
        success: (List<BusLineStopBean>) -> Unit,
        error: (String) -> Unit
    ) {
        service.getLineStationByLineId(lineId).doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.rows)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 查询用户所有订单
     */
    fun getOrderList(success: (List<BusOrderBean>) -> Unit, error: (String) -> Unit) {
        service.getOrderList().doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.rows)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 新增用户乘车订单
     */
    fun addOrder(
        busOrderBean: BusOrderBean,
        success: (code: Int) -> Unit,
        error: (String) -> Unit
    ) {
        service.addOrder(busOrderBean).doOnError { it.printStackTrace() }
            .subscribe {
                success(it.code)
                if (it.code != 200) {
                    error(it.msg)
                }
            }
    }

    /**
     * 支付订单
     */
    fun payOrder(
        busOrderBean: BusOrderBean,
        success: (code: Int) -> Unit, error: (String) -> Unit
    ) {
        service.payOrder(busOrderBean).doOnError { it.printStackTrace() }
            .subscribe {
                success(it.code)
                if (it.code != 200) error(it.msg)
            }
    }
}